<?php
class CdeUsersModule extends AppModel {

	var $name = 'CdeUsersModule';
	var $primaryKey = 'uid,cde_module_id';
	function getModuleInUnit($unitid=null) {
		$moduleinunit=array();
		$tempt=$this->query('SELECT id FROM cde_modules where cde_unit_id='.$unitid);
		foreach ($tempt as $module) {
			$moduleinunit[]=$module['cde_modules']['id'];
		}
		return $moduleinunit;
	}
	function getContinueModule($unitid=null,$userid=null) {
		$tempt=$this->query('SELECT cde_module_id FROM cde_users_modules where uid= '.$userid.' and cde_module_id in (select id FROM cde_modules where cde_unit_id='.$unitid .') order by cde_module_id DESC limit 1');
		return $tempt;
	}
	function addNewModule($moduleid=null,$userid=null) {
		$moduleExist=$this->query('SELECT * FROM cde_users_modules where cde_module_id='.$moduleid.' and uid='.$userid);
		$sql='insert into cde_users_modules values('.$userid.','.$moduleid.')';
		if (count($moduleExist)==0) {
			$this->query($sql);
		}
	}
	function delete($unitid=null,$userid=null) {
		$sql='delete from cde_users_modules where uid='.$userid.' and cde_module_id in (select id from cde_modules where cde_unit_id='.$unitid.')';
		$this->query($sql);
	}

}
?>